package cn.huapu.power.energy.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


@Component
public class Configurationn {

	private static Log log = LogFactory.getLog(Configurationn.class);



	/**
	 * configsetting配置路径
	 */
	private static final String CONFIGSETTING_FILE = "/config/configsetting.properties";


	/**
	 * 获取配置configsetting.Properties对象
	 *
	 * @return
	 */
	public static Properties getConfigsetting() {
		Properties properties = new Properties();
		InputStream is = null;
		try {
			is = Configurationn.class.getResourceAsStream(CONFIGSETTING_FILE);
			properties.load(is);
			return properties;
		} catch (FileNotFoundException e) {
			log.error("文件不存在", e);
		} catch (IOException e) {
			log.error("文件读取失败", e);
		} finally {
			try {
				if(is != null) {
					is.close();
				}
			} catch (IOException e) {
				log.error("文件流关闭异常", e);
			}
		}
		return null;
	}

	/**
	 * 获取配置Properties对象(通用方法)
	 * path 配置文件路径
	 * @return
	 */
	public static Properties getAppConfigAny(String path) {
		Properties properties = new Properties();
		InputStream is = null;
		try {
			is = Configurationn.class.getResourceAsStream(path);
			properties.load(is);
			return properties;
		} catch (FileNotFoundException e) {
			log.error("文件不存在", e);
		} catch (IOException e) {
			log.error("文件读取失败", e);
		} finally {
			try {
				if(is != null) {
					is.close();
				}
			} catch (IOException e) {
				log.error("文件流关闭异常", e);
			}
		}
		return null;
	}
}
